package com.et114.modules.org.dao;

import java.util.List;

import com.et114.components.acegi.Constants;
import com.et114.core.common.AppContext;
import com.et114.core.dao.impl.BaseDAOImpl;
import com.et114.core.exception.CheckException;
import com.et114.core.utility.StringPool;
import com.et114.modules.org.vo.GroupRelVo;
import com.et114.modules.org.vo.GroupRoleRelVO;
import com.et114.modules.org.vo.GroupVo;
import com.et114.modules.org.vo.UserEmplyVo;

//@author zhouxy , guanhw
public class GroupDao extends BaseDAOImpl {
	public void updateGroup ( GroupVo groupVo ) throws CheckException {
		update ( groupVo , "updateGroup" );
		if ( AppContext.isHasLog (  ) ) {		
        	groupVo.setOperType ( Constants.LOG_OPER_UPDATE) ;
			addLogGroup( groupVo ) ; 
		}
	}
	//TODO LOG
	public void updateGroupRel ( GroupRelVo groupRelVo ) throws CheckException {
		update ( groupRelVo , "updateGroupRel" );
		if ( AppContext.isHasLog (  ) ) {		
			groupRelVo.setOperType ( Constants.LOG_OPER_UPDATE) ;
			addLogGroupRel( groupRelVo ) ; 
		}
	}
	//TODO LOG
	public int removeGroup ( GroupVo groupVo ) throws CheckException {
		return delete ( groupVo , "deleteGroup" );
	}
	
    public int removeGroup(Long grpId , Long operuser) throws CheckException {
		if ( AppContext.isHasLog (  ) ) {		
    		GroupVo groupVo = this.loadGroup ( grpId );
        	groupVo.setOperType ( Constants.LOG_OPER_REMOVE) ;
        	groupVo.setOperUser ( operuser );
			addLogGroup( groupVo ) ; 
		}
        return delete(grpId, "deleteGroup");
    }
	
    public int removeGroupRel(Long grpId , Long operuser) throws CheckException {
		if ( AppContext.isHasLog (  ) ) {		
    		GroupRelVo groupRelVo = this.loadGroupRel ( grpId );
    		groupRelVo.setOperType ( Constants.LOG_OPER_REMOVE) ;
    		groupRelVo.setOperUser ( operuser );
    		addLogGroupRel( groupRelVo ) ; 
		}
        return delete(grpId, "deleteGroupRel");
    }
	
    public Long createGroup(GroupVo groupVo) throws CheckException {
		Long result = ( Long ) insert ( groupVo , "createGroup" );
		if ( AppContext.isHasLog (  ) ) {		
        	groupVo.setOperType ( Constants.LOG_OPER_NEW) ;
			addLogGroup( groupVo ) ; 
		}
        return  result;
    }
	
	
	public Long createGroupRel ( GroupRelVo groupRelVo ) throws CheckException {
		Long result = ( Long ) insert ( groupRelVo , "createGroupRel" );
		if ( AppContext.isHasLog (  ) ) {		
    		groupRelVo.setOperType ( Constants.LOG_OPER_NEW) ;
        	addLogGroupRel( groupRelVo ) ; 
		}
        return result;
    }
	
	public Long maxGrpNoByParentId( Long grpId ) throws CheckException {
		return ( Long )sqlMapClientTemplate.queryForObject( "maxGrpNoByParentId" , grpId ) ; 
	}
	
	public GroupVo loadRootGroup ( ) throws CheckException {
		return ( GroupVo ) load ( null , "loadRootGroup" );
	}
	
	public List queryChildGroups ( Long grpId ) throws CheckException {
		return queryList ( grpId , "queryChildGroups" );
	}
	
	public List queryTitlesOfGroup ( Long grpId ) throws CheckException {
		return queryList ( grpId , "queryTitlesOfGroup" );
	}
	
	public GroupVo loadGroup ( GroupVo groupVo ) throws CheckException {
		return ( GroupVo ) load ( groupVo , "loadGroup" );
	}
	
	public GroupRelVo loadGroupRel ( Long grpId ) throws CheckException {
		return ( GroupRelVo ) load ( grpId , "loadGroupRel" );
	}
	
	public GroupRelVo loadGroupRelByCode ( String grpCode ) throws CheckException {
		return ( GroupRelVo ) load ( grpCode , "loadGroupRelByCode" );
	}	
	
	
	public GroupVo loadGroup ( Long grpId ) throws CheckException {
		return ( GroupVo ) load ( grpId , "loadGroup" );
	}
	
	public GroupVo loadParentGroup ( Long grpId ) {
		return ( GroupVo ) load ( grpId , "loadParentGroup" );
	}
	
	public List getGroupsByIds ( Long[] grpIds ) throws CheckException {
		GroupVo groupVo = new GroupVo( ) ; 
		groupVo.setGrpIds ( grpIds ) ; 
		return queryList( groupVo  , "getGroupsByIds" ) ;
	}
	
	
	public List getGroupsByPath( GroupRelVo  grpRel ) throws CheckException {  
		return queryList( grpRel  , "getGroupsByPath" ) ;		
	}
	
	
	public List getGroupsByPaths( String[] grpPaths ) throws CheckException {
		GroupRelVo grv = new GroupRelVo( ) ;
		grv.setGrpPaths ( grpPaths ) ; 
		return queryList( grv  , "getGroupsByPaths" ) ;
	}

    
    public Long countGroups ( GroupVo groupVo ) throws CheckException {
		return getCount ( groupVo , "countGroups" );
	}
    
    public List getGroupTrees ( GroupVo groupVo ) throws CheckException {
		return queryList(groupVo,"getGroupTrees");
	}
    
    public void moveGroupRel ( GroupRelVo groupRelVo ) throws CheckException {
		if ( groupRelVo.getGrpId ( ).longValue ( ) != Constants.ORG_GROUP_BASE_VALUE ) {
			String fgPath = loadGroupRel ( groupRelVo.getFgrpId ( ) ) .getGrpPath ( ) ;
			groupRelVo.setGrpPath (  fgPath  +  StringPool.SLE + groupRelVo.getGrpId ( ) ) ;
		}    	
		if ( AppContext.isHasLog (  ) ) {		
    		groupRelVo.setOperType ( Constants.LOG_OPER_UPDATE) ;	    		
        	addLogGroupRel( groupRelVo ) ; 
		}
    	update( groupRelVo, "moveGroupRel");
	}
    
    public void addLogGroup( GroupVo groupVo )  throws CheckException { 
		insert ( groupVo , "addLogGroup" ) ;
	}
    
    public void addLogGroupRel( GroupRelVo groupRelVo )  throws CheckException { 
		insert ( groupRelVo , "addLogGroupRel" ) ;
	}
    
    public void updateEmpOfGroup (UserEmplyVo userEmplyVo ) throws CheckException {
    	update(userEmplyVo, "updateEmpOfGroup");
	}
    
    public List getEmpJobstate (UserEmplyVo userEmplyVo ) throws CheckException {
    	return queryList(userEmplyVo, "getEmpJobstate");
	}
    
    public Long countMoveGroups ( GroupRelVo groupRelVo ) throws CheckException {
		return getCount ( groupRelVo , "countMoveGroups" );
	}
    
	
	public List loadChildGroup ( Long grpId ) throws CheckException {
		return queryList ( grpId , "loadChildGroup" );
	}
	
	public List getRootGroup ( ) throws CheckException {
		return queryList ( new GroupVo ( ) , "getRootGroup" );
	}
	
	public List getChildGroup ( Long grpId ) throws CheckException {
		return queryList ( grpId , "getChildGroup" );
	}
    
	public List getRolesByGrpId ( Long grpId )  throws CheckException {
		return queryList( grpId , "getRolesByGrpId" ) ;
	}
	
	public int countRolesByGrpId ( Long grpId )  throws CheckException {
		return getCount( grpId , "countRolesByGrpId" ).intValue ( )  ;
	}
	
	
	public void createGroupRoleRel( GroupRoleRelVO groupRoleRel ) throws CheckException { 
		insert ( groupRoleRel , "createGroupRoleRel" ) ;
		if ( AppContext.isHasLog (  ) ) {		
			groupRoleRel.setOperType ( Constants.LOG_OPER_NEW) ;
			groupRoleRel.setOperUser ( groupRoleRel.getCrtUser ( ) );
			addLogGroupRoleRel( groupRoleRel ) ; 
		}
	}
	
	public void updateGroupRoleRel( GroupRoleRelVO groupRoleRel ) throws CheckException { 
		update ( groupRoleRel , "updateGroupRoleRel" ) ;
	}	
	
	public void deleteGroupRoleRelByRoleId( Long roleId , Long operuser ) throws CheckException {  
		GroupRoleRelVO groupRoleRel = new GroupRoleRelVO( ) ; 
		groupRoleRel.setRoleId ( roleId ) ; 
		groupRoleRel.setOperUser ( operuser ) ;
		groupRoleRel.setOperType ( Constants.LOG_OPER_REMOVE ) ;
	}	
	
	public void deleteGroupRoleRel( GroupRoleRelVO groupRoleRel ) throws CheckException {  
		delete( groupRoleRel , "removeGroupRoleRel"  ) ; 
		if ( AppContext.isHasLog (  ) ) {		
			groupRoleRel.setOperType ( Constants.LOG_OPER_REMOVE) ;
			addLogGroupRoleRel( groupRoleRel ) ; 
		}
	}
	
	public void addLogGroupRoleRel( GroupRoleRelVO groupRoleRel )  throws CheckException { 
		insert ( groupRoleRel , "addLogGroupRoleRel" ) ;
	}
	
	
	
}
